/*
 * Copyright 2007 Sam Hough
 * 
 * This file is part of GWTS.
 *
 * GWTS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * GWTS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GWTS.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.redspr.gwts;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.gargoylesoftware.htmlunit.html.DomNode;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlInlineFrame;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class HtmlInlineFrameHandler extends HtmlElementHandler {
    @Override
    void handle(Tryst tryst, DomNode node, HttpServletResponse res)throws IOException {
        PrintWriter out = res.getWriter();
        HtmlInlineFrame hif = (HtmlInlineFrame) node;
        // System.err.println(((HtmlPage)hif.getEnclosedPage()).asXml());
        out.println("<!-- iframe inline start -->");
        HtmlElement html = ((HtmlPage) hif.getEnclosedPage())
                .getDocumentHtmlElement();
        List<DomNode> l = html.getHtmlElementsByTagName("head");
        if (l.size() > 0) {
          //  tryst.fix(l.get(0), out);
        }

        l = html.getHtmlElementsByTagName("body");

        if (l.size() > 0) {
            DomNode f = l.get(0).getFirstDomChild();
            if (f != null) {
            tryst.fix(f, res); // don't want another form!
            }
        }
        out.println("<!-- iframe inline end -->");
       // out.println("</div>");
    }
}
